package com.adcc.swingui.util;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
@SuppressWarnings("unchecked")
public class I18n {
	public static final String I18N_STRING = "config.message.msginfo";
	public static final String I18N_MSG_STRING = "config.exception.errinfo";
	public static final ResourceBundle rb = I18n.getI18N(ResUtil.currLocale);
	public static final ResourceBundle errrb = I18n.getErrI18N(ResUtil.currLocale);

	private I18n() {
	}

	/**
	 * 获取i18n对象
	 * 
	 * @param basename
	 *            文件路径相对src目录
	 * @param locale
	 *            本地语言
	 * @return 资源对象
	 */
	public static ResourceBundle getI18N(String basename, Locale locale) {
		ResourceBundle rb;
		try {
			if (locale == null) { // offline builder
				rb = ResourceBundle.getBundle(basename);
			} else { // online builder
				rb = ResourceBundle.getBundle(basename, locale);
			}

		} catch (java.util.MissingResourceException e) {
			rb = ResourceBundle.getBundle(basename, Locale.ENGLISH);
		}
		return rb;
	}

	/**
	 * 获取i18n对象
	 * 
	 * @param basename
	 *            文件路径相对src目录
	 * @return
	 */
	public static ResourceBundle getI18N(String basename) {
		ResourceBundle rb;
		try {
			rb = ResourceBundle.getBundle(basename);
		} catch (java.util.MissingResourceException e) {
			rb = ResourceBundle.getBundle(basename, Locale.ENGLISH);
		}
		return rb;
	}

	/**
	 * 获取i18n对象
	 * 
	 * @param basename
	 *            文件路径相对src目录
	 * @return
	 */
	public static ResourceBundle getI18N() {
		ResourceBundle rb;
		try {
			rb = ResourceBundle.getBundle(I18N_STRING);
		} catch (java.util.MissingResourceException e) {
			rb = ResourceBundle.getBundle(I18N_STRING, Locale.ENGLISH);
		}
		return rb;
	}
	/**
	 * 获取i18n对象，根据配置的语言
	 * @param locale
	 * @return
	 */
	public static ResourceBundle getI18N(Locale locale) {
		ResourceBundle rb;
		try {
			rb = ResourceBundle.getBundle(I18N_STRING, locale);
		} catch (java.util.MissingResourceException e) {
			rb = ResourceBundle.getBundle(I18N_STRING, Locale.ENGLISH);
		}
		return rb;
	}
	
	/**
	 * 获取异常i18n对象，根据配置的语言
	 * @param locale
	 * @return
	 */
	public static ResourceBundle getErrI18N() {
		ResourceBundle rb;
		try {
			rb = ResourceBundle.getBundle(I18N_MSG_STRING);
		} catch (java.util.MissingResourceException e) {
			rb = ResourceBundle.getBundle(I18N_MSG_STRING, Locale.ENGLISH);
		}
		return rb;
	}
	
	/**
	 * 获取异常i18n对象，根据配置的语言
	 * @param locale
	 * @return
	 */
	public static ResourceBundle getErrI18N(Locale locale) {
		ResourceBundle rb;
		try {
			rb = ResourceBundle.getBundle(I18N_MSG_STRING, locale);
		} catch (java.util.MissingResourceException e) {
			rb = ResourceBundle.getBundle(I18N_MSG_STRING, Locale.ENGLISH);
		}
		return rb;
	}

	/**
	 * JButton 国际化
	 * 
	 * @param button
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JButton button, ResourceBundle b, String clsname, String name) {
		button.setText(getLabel(b, clsname, name));
		String key;
		key = getKey(b, clsname, name);
		if (key != null)
			button.setMnemonic(key.charAt(0));

		String index;
		index = getIndex(b, clsname, name);
		if (index != null) {
			button.setDisplayedMnemonicIndex(Integer.parseInt(index));
		}
	}

	/**
	 * JMenu 国际化
	 * 
	 * @param menuItem
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JMenu menu, ResourceBundle b, String clsname, String name) {
		menu.setText(getLabel(b, clsname, name));
		String key;
		key = getKey(b, clsname, name);
		if (key != null)
			menu.setMnemonic(key.charAt(0));
	}

	/**
	 * JMenuItem 国际化
	 * 
	 * @param menu
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JMenuItem menuItem, ResourceBundle b, String clsname, String name) {
		menuItem.setText(getLabel(b, clsname, name));
		String key;
		key = getKey(b, clsname, name);
		if (key != null)
			menuItem.setMnemonic(key.charAt(0));

		String index;
		index = getIndex(b, clsname, name);
		if (index != null)
			menuItem.setDisplayedMnemonicIndex(Integer.parseInt(index));
	}

	/**
	 * JLabel 国际化
	 * 
	 * @param label
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JLabel label, ResourceBundle b, String clsname, String name) {
		label.setText(getLabel(b, clsname, name));
		String key;
		key = getKey(b, clsname, name);
		if (key != null)
			label.setDisplayedMnemonic(key.charAt(0));

		String index;
		index = getIndex(b, clsname, name);
		if (index != null)
			label.setDisplayedMnemonicIndex(Integer.parseInt(index));

	}

	/**
	 * JRadioButton 国际化
	 * 
	 * @param radioButton
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JRadioButton radioButton, ResourceBundle b, String clsname, String name) {
		radioButton.setText(getLabel(b, clsname, name));
		String key;
		key = getKey(b, clsname, name);
		if (key != null)
			radioButton.setMnemonic(key.charAt(0));
		String index;
		index = getIndex(b, clsname, name);
		if (index != null)
			radioButton.setDisplayedMnemonicIndex(Integer.parseInt(index));
	}

	/**
	 * JCheckBox 国际化
	 * 
	 * @param checkBox
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JCheckBox checkBox, ResourceBundle b, String clsname, String name) {
		checkBox.setText(getLabel(b, clsname, name));
		String key;
		key = getKey(b, clsname, name);
		if (key != null)
			checkBox.setMnemonic(key.charAt(0));
		String index;
		index = getIndex(b, clsname, name);
		if (index != null)
			checkBox.setDisplayedMnemonicIndex(Integer.parseInt(index));
	}

	/**
	 * TitledBorder 国际化
	 * 
	 * @param titledBorder
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(TitledBorder titledBorder, ResourceBundle b, String clsname, String name) {
		titledBorder.setTitle(getLabel(b, clsname, name));
	}

	/**
	 * JTabbedPane 国际化
	 * 
	 * @param tabbedPane1
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JTabbedPane tabbedPane1, ResourceBundle b, String clsname, String panelname) {
		for (int i = 0; i < tabbedPane1.getTabCount(); i++) {
			tabbedPane1.setTitleAt(i, getLabel(b, clsname, panelname + Integer.toString(i)));
		}
	}

	/**
	 * JTable 国际化
	 * 
	 * @param table
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JTable table, ResourceBundle b, String clsname, String tablname) {
		setBundle(table, b, clsname, tablname, null);
	}

	/**
	 * JTable DefaultTableModel 国际化
	 * 
	 * @param table
	 * @param tblM
	 * @param menu
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JTable table, DefaultTableModel tblM, ResourceBundle b, String clsname, String tablname) {
		Vector colName = new Vector();
		setBundle(table, b, clsname, tablname, colName);
		tblM.setColumnIdentifiers(colName);
	}

	/**
	 * JTable tablname 国际化
	 * 
	 * @param table
	 * @param b
	 * @param clsname
	 * @param tablname
	 * @param colName
	 */
	private static void setBundle(JTable table, ResourceBundle b, String clsname, String tablname, Vector colName) {
		TableColumnModel tblColM = table.getColumnModel();
		FontMetrics fm = table.getFontMetrics(table.getFont());
		int tblCount = tblColM.getColumnCount();
		for (int i = 0; i < tblCount; i++) {
			TableColumn tblCol = tblColM.getColumn(i);
			String s = getLabel(b, clsname, tablname + Integer.toString(i));
			tblCol.setHeaderValue(s);
			if (colName != null) {
				colName.add(s);
			}
			int colWidth = getColLength(b, clsname, tablname + Integer.toString(i));
			if (colWidth <= 0) {
				colWidth = fm.stringWidth(s);
				colWidth = colWidth + table.getFont().getSize();
				if (colWidth > tblCol.getPreferredWidth()) {
					tblCol.setPreferredWidth(colWidth);
				}
			} else {
				colWidth = (colWidth + 1) * fm.stringWidth("W");
				tblCol.setPreferredWidth(colWidth);
			}
		}
	}

	/**
	 * JComboBox 国际化
	 * 
	 * @param comboBox
	 * @param b
	 *            ResourceBundle
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 */
	public static void setBundle(JComboBox comboBox, ResourceBundle b, String clsname, String name) {
		clsname = replaceClass(clsname);
		String label;
		for (int i = 0;; i++) {
			try {
				label = b.getString(clsname + "." + name + Integer.toString(i) + ".label");
			} catch (MissingResourceException e) {
				return;
			}
			comboBox.addItem(label);
		}
	}

	/**
	 * JDialog 国际化
	 * 
	 * @param jDialog
	 * @param b
	 * @param clsname
	 *            类名
	 * @param name
	 *            控件名
	 * @param replace
	 *            参数
	 */
	public static void setBundle(JDialog jDialog, ResourceBundle b, String clsname, String name, String replace) {
		String label = getLabel(b, clsname, name);
		jDialog.setTitle(label.replaceFirst("[%]", replace));
	}

	/**
	 * JDialog 国际化
	 * 
	 * @param jDialog
	 * @param b
	 * @param clsname
	 * @param name
	 */
	public static void setBundle(JDialog jDialog, ResourceBundle b, String clsname, String name) {
		String label = getLabel(b, clsname, name);
		jDialog.setTitle(label);
	}

	/**
	 * jFrame 国际化
	 * 
	 * @param jDialog
	 * @param b
	 * @param clsname
	 * @param name
	 */
	public static void setBundle(JFrame jFrame, ResourceBundle b, String clsname, String name) {
		String label = getLabel(b, clsname, name);
		jFrame.setTitle(label);
	}

	/**
	 * 获取.label结尾的配置
	 * 
	 * @param b
	 * @param clsname
	 * @param name
	 * @return
	 */
	public static String getLabel(ResourceBundle b, String clsname, String name) {
		String label;
		clsname = replaceClass(clsname);

		try {
			label = b.getString(clsname + "." + name + ".label");
		} catch (MissingResourceException e) {
			label = name;
		}
		return label;
	}

	/**
	 * 获取.key结尾的配置
	 * 
	 * @param b
	 * @param clsname
	 * @param name
	 * @return
	 */
	public static String getKey(ResourceBundle b, String clsname, String name) {
		clsname = replaceClass(clsname);
		String key = null;

		try {
			key = b.getString(clsname + "." + name + ".key");
		} catch (MissingResourceException e) {
			key = null;
		}
		return key;
	}

	/**
	 * 获取.index结尾的配置
	 * 
	 * @param b
	 * @param clsname
	 * @param name
	 * @return
	 */
	public static String getIndex(ResourceBundle b, String clsname, String name) {
		clsname = replaceClass(clsname);
		String index = null;

		try {
			index = b.getString(clsname + "." + name + ".index");
		} catch (MissingResourceException e) {
			index = null;
		}
		return index;
	}

	/**
	 * 获取.length结尾的配置
	 * 
	 * @param b
	 * @param clsname
	 * @param name
	 * @return
	 */
	public static int getColLength(ResourceBundle b, String clsname, String name) {
		clsname = replaceClass(clsname);
		String length = null;
		int column = -1;

		try {
			length = b.getString(clsname + "." + name + ".length");
		} catch (MissingResourceException e) {
		}

		if (length != null) {
			column = Integer.parseInt(length);
		}
		return column;
	}

	private static String replaceClass(String clsname) {
		if (clsname == null) {
			return "";
		}
		return clsname.replaceFirst("class ", "");

	}

	public static void main(String[] args) {
		JFrame t18 = new JFrame();
		JButton jButton = new JButton();
		t18.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JLabel jLabel = new JLabel();
		JRadioButton jRadioButton = new JRadioButton();
		JComboBox jComboBox = new JComboBox();
		JTable jTable = null;
		JScrollPane jScrollPane = new JScrollPane();

		t18.setSize(800, 600);
		String[] columnNames = { "b", "a" };
		Object[][] data = { { "Apples", new Integer(5) }, { "Oranges", new Integer(3) } };
		// t18.setLayout(new BorderLayout());
		jTable = new JTable(data, columnNames);
		jTable.setPreferredScrollableViewportSize(new Dimension(300, 80));

		I18n.setBundle(jButton, rb, I18n.class.toString(), "jButton");
		I18n.setBundle(jRadioButton, rb, I18n.class.toString(), "jRadioButton");
		I18n.setBundle(jComboBox, rb, I18n.class.toString(), "jComboBox");
		I18n.setBundle(jLabel, rb, I18n.class.toString(), "jLabel");
		I18n.setBundle(jTable, rb, I18n.class.toString(), "jTable");
		t18.setLayout(new FlowLayout());
		t18.add(jButton);
		t18.add(jRadioButton);
		t18.add(jComboBox);
		jScrollPane.setViewportView(jTable);
		t18.add(jScrollPane);
		t18.setVisible(true);

	}

}
